package com.zmj.framework.dao.jdbc;

import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.zmj.framework.bean.Code;
import com.zmj.framework.dao.CodeDao;
import com.zmj.framework.util.FrameContext;
@Repository
public class CodeDaoJdbc implements CodeDao {
	@Autowired
	FrmJdbcTemplate jdbcTemplate;

	public CodeDaoJdbc() {
		this.jdbcTemplate = null;
	}

	public int removeCode(Code frmCode) throws SQLException {
		int ret=0;
		ret=jdbcTemplate.update("delete from FRM_CODE where dmlb='"+frmCode.getDmlb()+"' and dmz='"+frmCode.getDmz()+"'");  
		return ret;
	}

	public int saveCode(Code frmCode) throws SQLException {
		
		int ret=0;
		ret=removeCode(frmCode);
		ret=jdbcTemplate.update("insert into FRM_CODE(dmlb, dmz, dmsm1, dmsm2, dmsm3, dmsm4, dmsx, zt) values('"  
		           + frmCode.getDmlb() + "', '"  
		           + frmCode.getDmz() + "', '"  
		           + frmCode.getDmsm1() + "', '"  
		           + frmCode.getDmsm2() + "', '"  
		           + frmCode.getDmsm3() + "', '"  
		           + frmCode.getDmsm4() + "', '"  
		           + frmCode.getDmsx() + "', '"  
		           + frmCode.getZt() + "')");  
		return ret;
	}

	public Code getCode(String dmlb, String dmz) {
		List list = getCodes(dmlb);
		Code code = null;
		for (int i = 0; i < list.size(); ++i) {
			Code tmpcode = (Code) list.get(i);
			if (tmpcode.getDmz().equals(dmz)) {
				code = tmpcode;
				break;
			}
		}
		return code;
	}

	public List getCodes(String dmlb) throws DataAccessException {
		List CodeList = null;
		String key = "code:" + dmlb;

		String strSql = "select * from frm_code where dmlb='" + dmlb + "' order by dmz";

		CodeList = this.jdbcTemplate.queryForList(strSql, Code.class);
		FrameContext.getInstance().reg(key, CodeList);

		return CodeList;
	}
}